﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.RadicadoInterno;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Core.AssociatedActions;
using Microsoft.SharePoint;
using MS.InformationManagement.Support;
using MS.InformationManagement.Core.Documentset;
using MS.InformationManagement.DAL.DocumentCenter;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.Presenter.InsertarRadicadoInterno
{
    public class RadicadoInternoPresenter : BasePresenter<IRadicadoInternoView>
    {

        #region Constructors

        public RadicadoInternoPresenter(IRadicadoInternoView view) : base(view) { }

        #endregion

        public void HandleSave()
        {

            if (View.AssociatedRadicados == null && string.IsNullOrEmpty(View.AssociatedAction))
                View.OperationResponse = new GestionRadicadoInterno().SaveRadicadoInterno(View.RadicadoEntity);
            else
            {
                string targetLibrary = SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain();// View.RadicadoEntity.SelectedDestinatario[0].User.LoginName.GetUserWithOutDomain(); 
                IRadicado radObject = new RadicadoInternoActions(View.RadicadoEntity) { CurrentLibrary = UserLibraryHelper.CurrentUserLibraryName, CurrentContentType = GlobalConstants.CTTN_RADICADO_INTERNO };
                View.OperationResponse = new ExpedienteAsociation(View.AssociatedRadicados[0], null, targetLibrary, radObject, (Radicado)View.RadicadoEntity, View.SubjectExpediente).Execute();
            }
            View.RadicadoGenerated();

        }

        public void HandleUpdate()
        {
            new GestionRadicadoInterno().UpdateRadicadoInterno(View.RadicadoEntity);
            View.RadicadoUpdated();
        }

        public void HandleLoad()
        {
            if (View.CurrentId.HasValue)
            {
                View.RadicadoEntity = new GestionRadicadoInterno().GetRadicadoInterno(View.CurrentId.Value, View.CurrentList);
                View.Show();
                if(View.Strategy == "view")
                    View.DisableControls();
                if (View.Strategy == "edit")
                    View.ShowForEdit();
            }

            if (View.AssociatedAction == "new")
            {
                //View.AssociatedRadicadosData = RadicadosHelper.GetRadicadoByIdCurrentLibrary(View.AssociatedRadicados);
                View.AssociatedRadicadosData = new List<Radicado>();
                View.AssociatedRadicadosData.Add(RadicadosHelper.GetRadicadoById(View.AssociatedRadicados[0]));
                if (!string.IsNullOrEmpty(View.AssociatedRadicadosData[0].NúmeroExpediente))
                    View.SubjectExpediente = RadicadosHelper.GetExpedienteSubjectById(View.AssociatedRadicadosData[0].NúmeroExpediente);
                View.LoadForAnexo();

            }
        }
    }
}
